package org.inspursc.s0517.health.common.dao;

import org.inspursc.s0517.health.common.entity.SysPermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface SysPermissionDao extends JpaRepository<SysPermission, Integer> {

    @Query(value = "select p.* from sys_user u LEFT JOIN sys_role_user sru on u.id= sru.sys_user_id " +
            " LEFT JOIN sys_role r on sru.sys_role_id=r.id " +
            " LEFT JOIN sys_group_role sgr on sgr.sys_role_id=r.id " +
            " LEFT JOIN sys_permisson_group spg on spg.sys_group_id=sgr.sys_group_id" +
            " LEFT JOIN sys_permission p on p.id =spg.sys_permission_id " +
            " where u.id=?1", nativeQuery = true)
    List<SysPermission> findByUserId(Integer userId);


    @Query(value = "select p.* from sys_permission p " +
            " LEFT JOIN sys_permission_group spg on p.id= spg.sys_permission_id " +
            " LEFT JOIN sys_group_role sgr on spg.sys_group_id=sgr.sys_group_id " +
            " LEFT JOIN sys_role r on sgr.sys_role_id=r.id " +
            " where r.id=?1", nativeQuery = true)
    List<SysPermission> findByRoleId(Integer roleId);


    @Query(value = "select p.* from sys_permission p " +
            " LEFT JOIN sys_permission_group spg on p.id= spg.sys_permission_id " +
            " where spg.sys_group_id=?1", nativeQuery = true)
    List<SysPermission> findByGroupId(Integer groupId);
}
